package com.wondersgroup.framework.widget.taglib.tree;

import com.wondersgroup.framework.widget.annotations.WidgetTag;
import com.wondersgroup.framework.widget.annotations.WidgetTagAttribute;
import com.wondersgroup.framework.widget.library.extjs.ExtComponent;
import com.wondersgroup.framework.widget.library.extjs.ExtTreePanel;
import com.wondersgroup.framework.widget.library.extjs.PlainTextComponent;
import com.wondersgroup.framework.widget.library.html.Div;
import com.wondersgroup.framework.widget.library.html.HtmlComponent;
import com.wondersgroup.framework.widget.taglib.WidgetTagSupport;
import com.wondersgroup.framework.widget.util.OutputUtils;
import com.wondersgroup.framework.widget.util.TagUtils;
import com.wondersgroup.framework.widget.validation.annotation.TypeValidate;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;

@WidgetTag(name = "tree", tldTagClass = "com.wondersgroup.framework.widget.taglib.tree.TreeTag", description = "Tree提供了一个灵活简单的tree控件，它支持静态定义树节点，也支持从服务端获取节点")
public class TreeTag extends WidgetTagSupport {
	private String width = "0";
	private int height;
	private int level;

	@TypeValidate
	private boolean animate;
	private String url;
	private String urlParams;
	private String data;
	private String rootText = "根目录";

	@TypeValidate
	private boolean rootVisible = true;

	@TypeValidate
	private boolean enableDD;

	@TypeValidate
	private boolean allowEdit;

	@TypeValidate
	private boolean editable;
	private String form = "form";
	private String formLoadAction;
	private String formAddAction;
	private String formUpdateAction;
	private String formDeleteAction;
	private String onBeforeLoad;
	private String onCheckChange;
	private String onClick;
	private String contextMenu;

	public String getWidth() {
		return this.width;
	}

	@WidgetTagAttribute(description = "树宽度,默认自适应", type = "String")
	public void setWidth(String paramInt) {
		this.width = paramInt;
	}

	public int getHeight() {
		return this.height;
	}

	@WidgetTagAttribute(description = "树高度，默认自适应", type = "int")
	public void setHeight(int paramInt) {
		this.height = paramInt;
	}

	public int getLevel() {
		return this.level;
	}

	@WidgetTagAttribute(description = "默认展开的层数", type = "int")
	public void setLevel(int paramInt) {
		this.level = paramInt;
	}

	public boolean isAnimate() {
		return this.animate;
	}

	@WidgetTagAttribute(description = "展开时候是否出现动画,默认false", type = "boolean")
	public void setAnimate(boolean paramBoolean) {
		this.animate = paramBoolean;
	}

	public String getUrl() {
		return this.url;
	}

	@WidgetTagAttribute(description = "远程访问url")
	public void setUrl(String paramString) {
		this.url = paramString;
	}

	public String getUrlParams() {
		return this.urlParams;
	}

	@WidgetTagAttribute(description = "访问url的基础参数")
	public void setUrlParams(String paramString) {
		this.urlParams = paramString;
	}

	public String getData() {
		return this.data;
	}

	@WidgetTagAttribute(description = "本地数据源")
	public void setData(String paramString) {
		this.data = paramString;
	}

	public String getRootText() {
		return this.rootText;
	}

	@WidgetTagAttribute(description = "根目录的名称,默认'根目录'")
	public void setRootText(String paramString) {
		this.rootText = paramString;
	}

	public boolean isRootVisible() {
		return this.rootVisible;
	}

	@WidgetTagAttribute(description = "根目录是否可见,默认true", type = "boolean")
	public void setRootVisible(boolean paramBoolean) {
		this.rootVisible = paramBoolean;
	}

	public boolean isEnableDD() {
		return this.enableDD;
	}

	@WidgetTagAttribute(description = "节点是否允许拖拽,默认false", type = "boolean")
	public void setEnableDD(boolean paramBoolean) {
		this.enableDD = paramBoolean;
	}

	public boolean isAllowEdit() {
		return this.allowEdit;
	}

	@WidgetTagAttribute(description = "是否节点允许编辑,默认false", type = "boolean")
	public void setAllowEdit(boolean paramBoolean) {
		this.allowEdit = paramBoolean;
	}

	public String getOnBeforeLoad() {
		return this.onBeforeLoad;
	}

	@WidgetTagAttribute(description = "节点加载前方法调用")
	public void setOnBeforeLoad(String paramString) {
		this.onBeforeLoad = paramString;
	}

	public String getOnCheckChange() {
		return this.onCheckChange;
	}

	@WidgetTagAttribute(description = "节点选择/反选时方法调用")
	public void setOnCheckChange(String paramString) {
		this.onCheckChange = paramString;
	}

	public String getOnClick() {
		return this.onClick;
	}

	@WidgetTagAttribute(description = "节点单击方法调用")
	public void setOnClick(String paramString) {
		this.onClick = paramString;
	}

	public boolean isEditable() {
		return this.editable;
	}

	@WidgetTagAttribute(description = "是否是可编辑的树，如果是会产生操作工具栏,并且树的onclick事件无效。默认为false", type = "boolean")
	public void setEditable(boolean paramBoolean) {
		this.editable = paramBoolean;
	}

	public String getForm() {
		return this.form;
	}

	@WidgetTagAttribute(description = "编辑树对应的formId,默认为'form',在editable = true的前提下有效")
	public void setForm(String paramString) {
		this.form = paramString;
	}

	public String getFormLoadAction() {
		return this.formLoadAction;
	}

	@WidgetTagAttribute(description = "编辑树对应的form节点加载时候的触发事件url,在editable = true的前提下有效")
	public void setFormLoadAction(String paramString) {
		this.formLoadAction = paramString;
	}

	public String getFormAddAction() {
		return this.formAddAction;
	}

	@WidgetTagAttribute(description = "编辑树对应的form节点新增时候的触发事件url,在editable = true的前提下有效")
	public void setFormAddAction(String paramString) {
		this.formAddAction = paramString;
	}

	public String getFormUpdateAction() {
		return this.formUpdateAction;
	}

	@WidgetTagAttribute(description = "编辑树对应的form节点修改时候的触发事件url,在editable = true的前提下有效")
	public void setFormUpdateAction(String paramString) {
		this.formUpdateAction = paramString;
	}

	public String getFormDeleteAction() {
		return this.formDeleteAction;
	}

	@WidgetTagAttribute(description = "编辑树对应的form节点删除时候的触发事件url,在editable = true的前提下有效")
	public void setFormDeleteAction(String paramString) {
		this.formDeleteAction = paramString;
	}

	public String getContextMenu() {
		return this.contextMenu;
	}

	@WidgetTagAttribute(description = "[可选]上下文菜单的ID")
	public void setContextMenu(String paramString) {
		this.contextMenu = paramString;
	}

	public void processTagContent() throws JspException {
		Div localDiv = new Div(getId());
		ExtTreePanel localExtTreePanel = new ExtTreePanel(getId());
		BeanUtils.copyProperties(this, localExtTreePanel);
		localExtTreePanel.setEl(getId());
		localExtTreePanel.setAutoScroll(true);
		localExtTreePanel.setBorder(true);
		localExtTreePanel.setUseArrows(true);
		localExtTreePanel.setContainerScroll(true);
		if (localExtTreePanel.getHeight()==0) {
			localExtTreePanel.setAutoHeight(true);
		}
			
		if (isEditable()) {
			localExtTreePanel
					.setTbar("[{text:'增加',id: '"
							+ getId()
							+ "_addButton',handler:function(){Wonders.EditableTree.addClick('"
							+ getForm()
							+ "','"
							+ getId()
							+ "');}},"
							+ "{text:'删除',id: '"
							+ getId()
							+ "_delButton',handler:function(){Wonders.EditableTree.delClick('"
							+ getForm()
							+ "','"
							+ getId()
							+ "','"
							+ getFormDeleteAction()
							+ "');}},"
							+ "{text:'修改',id: '"
							+ getId()
							+ "_modifyButton',handler:function(){Wonders.EditableTree.modifyClick('"
							+ getForm()
							+ "','"
							+ getId()
							+ "');}},"
							+ "'-',"
							+ "{text:'保存',id: '"
							+ getId()
							+ "_saveButton',disabled: true,handler:function(){Wonders.EditableTree.saveClick('"
							+ getForm()
							+ "','"
							+ getId()
							+ "','"
							+ getFormAddAction()
							+ "','"
							+ getFormUpdateAction()
							+ "');}},"
							+ "{text:'取消',id: '"
							+ getId()
							+ "_cancelButton',disabled: true,handler:function(){Wonders.EditableTree.cancelClick('"
							+ getForm() + "','" + getId() + "');}}]");
			if (localExtTreePanel.getOnClick() == null) {
				localExtTreePanel
					.setOnClick("function(node,e){Wonders.EditableTree.nodeClick(node,'"
							+ getForm()
							+ "','"
							+ getId()
							+ "','"
							+ getFormLoadAction() + "');}");
			}
		}
		localExtTreePanel.setContextMenu(getContextMenu());
		PlainTextComponent localPlainTextComponent = new PlainTextComponent();
		if (isAllowEdit()) {
			localPlainTextComponent.setContent("new cf_t.TreeEditor(" + getId() + ",{});");
		}
			
		StringBuffer localStringBuffer = new StringBuffer();
		localStringBuffer.append(OutputUtils.writeHTML(new HtmlComponent[] { localDiv }));
		localStringBuffer.append(OutputUtils.writeJSFile(this.pageContext, new String[] { "extjs/tree/tree.js", "extjs/form/form.js" }));
		if (isEditable()) {
			localStringBuffer.append(OutputUtils.writeJSFile(this.pageContext, new String[] { "wonders/tree/wonders-tree-min.js" }));
		}
			
		localStringBuffer.append(OutputUtils.ExtOnReady(new ExtComponent[] {localExtTreePanel, localPlainTextComponent }));
		if (this.pageContext.getAttribute("wonders.treeField.tree") != null) {
			this.pageContext.setAttribute("wonders.treeField.tree", localExtTreePanel);
		} else {
			TagUtils.getInstance().write(this.pageContext, localStringBuffer.toString());
		}	
	}
}